package com.cn.jdia.tas.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cn.jdia.tas.context.SpringContextUtils;
import com.cn.jdia.tas.model.U_department;
import com.cn.jdia.tas.module.SimpleDeptModule;
import com.cn.jdia.tas.service.IDepartmentService;
import com.google.gson.Gson;

@Controller
@RequestMapping("/control")
public class DepartmentController {

	@Resource
	private IDepartmentService service = null;

	@RequestMapping("/dept")
	public String getDeptData(HttpServletRequest request, HttpServletResponse response) throws IOException {

		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		List<U_department> deptList = service.getDeptInfo();
		System.out.println(deptList.toString());

		String deptid = null;
		String deptname = null;
		String parentid = null;

		List<U_department> secList = new ArrayList<>();
		Map<String,SimpleDeptModule> map=new HashMap<>();
		
		for (U_department dept : deptList) {
			deptid = dept.getDeptid();
			deptname = dept.getDeptname();
			parentid = dept.getParentdeptid();

			if (parentid.equals("1")) {
				SimpleDeptModule sim = new SimpleDeptModule();
				
				sim.setValue(deptid);
				sim.setText(deptname);
				
				map.put(deptid, sim);
			} else {
				secList.add(dept);
			}
		}

		for (U_department dept : secList) {
			deptid = dept.getDeptid();
			deptname = dept.getDeptname();
			parentid = dept.getParentdeptid();
			
			SimpleDeptModule sim = map.get(parentid);
			List<SimpleDeptModule> children = sim.getChildren();
			
			SimpleDeptModule child=new SimpleDeptModule();
			child.setValue(deptid);
			child.setText(deptname);
			
			children.add(child);
			
			sim.setChildren(children);
		}
		
		List<SimpleDeptModule> retList=new ArrayList<>();
		for(String key:map.keySet()){
			retList.add( map.get(key));
		}
		
		Gson gson = new Gson();
		String retDate = gson.toJson(retList);
		System.out.println(retDate);
		
		out.write(retDate);
		out.flush();
		out.close();

		return null;
	}
	
	@RequestMapping("/deptlist")
	public String getDeptDataWithNoChild(HttpServletRequest request, HttpServletResponse response) throws IOException {

		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		List<U_department> deptList = service.getDeptInfo();
//		System.out.println(deptList.toString());

		String deptid = null;
		String deptname = null;
		
		List<Map<String,String>> retList=new ArrayList<>();
		Map<String,String> map;
		
		for (U_department dept : deptList) {
			deptid = dept.getDeptid();
			deptname = dept.getDeptname();

			System.out.println(deptid+deptname);
			map=new HashMap<>();
			map.put("value", dept.getDeptid());
			map.put("text", dept.getDeptname());
			
			retList.add(map);
		}
		
		Gson gson = new Gson();
		
		out.write(gson.toJson(retList));
		out.flush();
		out.close();

		return null;
	}
}
